package com.hs.srm.admin.modules.material.service.impl;

import java.util.List;
import java.util.Map;

import com.hs.srm.admin.modules.material.entity.MaterialFactoryEntity;
import com.hs.srm.admin.modules.material.service.MaterialFactoryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.modules.material.dao.MaterialDao;
import com.hs.srm.admin.modules.material.entity.MaterialEntity;
import com.hs.srm.admin.modules.material.service.MaterialService;
import com.hs.srm.base.common.annotation.DataFilter;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.common.utils.PageUtils;

@Service("materialService")
public class MaterialServiceImpl extends ServiceImpl<MaterialDao, MaterialEntity> implements MaterialService {
    @Autowired
    private MaterialFactoryService materialFactoryServiceImpl;

    @Override
    @DataFilter
    public PageUtils queryPage(Map<String, Object> params) {
        String materialCode = (String) params.get("materialCode");
        String materialGroup = (String) params.get("materialGroup");
        String materialDesc = (String) params.get("materialDesc");
        String industrySector = (String) params.get("industrySector");
        String materialStatus = (String) params.get("materialStatus");
        String createdBy = (String) params.get("createdBy");
        String creationDate = (String) params.get("creationDate");
        Page<MaterialEntity> page = this.selectPage(
                new QueryParam<MaterialEntity>(params).getPage(),
                new EntityWrapper<MaterialEntity>()
                        .like(StringUtils.isNotBlank(materialCode), "material_code ", materialCode)
                        .eq("isdel ", 0)
                        .eq(StringUtils.isNotBlank(materialGroup), "material_group ", materialGroup)
                        .like(StringUtils.isNotBlank(materialDesc), "material_desc", materialDesc)
                        .eq(StringUtils.isNotBlank(industrySector), "industry_sector", industrySector)
                        .eq(StringUtils.isNotBlank(materialStatus), "material_status", materialStatus)
                        .eq(StringUtils.isNotBlank(createdBy), "created_by", createdBy)
                        .eq(StringUtils.isNotBlank(creationDate), "creation_date", creationDate)

        );
        return new PageUtils(page);
    }

    /**
     * @Description 物料数据保存方法
     * @Param [entity]
     * @Return boolean
     * @Author socik
     * @Date 2019/5/30 9:43
     */

    @Override
    public boolean insertMaterial(MaterialEntity entity) {
        boolean flg = this.insert(entity);
        return flg;
    }

    @Override
    public MaterialEntity selectMaterialByCode(String code) {
        EntityWrapper<MaterialEntity> queryWrapper = new EntityWrapper<>();
        queryWrapper.eq("material_code", code);
        MaterialEntity entity = this.selectOne(queryWrapper);
        return entity;
    }

    @Override
    public boolean updateMaterial(MaterialEntity entity) {
        boolean flg = this.updateById(entity);
        return flg;
    }

    /**
     * @Description 物料删除方法
     * @Param [code]
     * @Return boolean
     * @Author socik
     * @Date 2019/5/30 9:43
     */

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean deleteMaterialByCode(String code) {
        //根据物料号查询所有数据
        MaterialEntity entity = this.selectMaterialByCode(code);
        boolean flg = false;
        //判断是否查询到数据
        if (entity != null) {
            //设置删除标记为1
            entity.setIsdel(1);
            flg = this.updateMaterial(entity);
        }
        //查询物料工厂数据列表
        List<MaterialFactoryEntity> entity1 = this.materialFactoryServiceImpl.selectMaterialFactoryListByCode(code);
        //循环设置删除标记
        for (MaterialFactoryEntity factoryEntity : entity1) {
            factoryEntity.setIsdel(1);
            flg = this.materialFactoryServiceImpl.deleteMaterialFactory(factoryEntity);
        }
        //判断是否更新数据成功
        if (!flg) {
            throw new RuntimeException("更新出错，数据回滚!");
        }
        return flg;
    }

    /**
     * @Description 查询最大物料号
     * @Param []
     * @Return java.lang.String
     * @Author socik
     * @Date 2019/5/30 9:46
     */

    @Override
    public String selectMaterialMaxCode() {
        EntityWrapper<MaterialEntity> queryWrapper = new EntityWrapper<>();
        //查询排除废弃料号和不可用料号
        queryWrapper.setSqlSelect("Max(material_code)").notIn("material_type", "DIEN");
        String maxCode = (String) this.selectObj(queryWrapper);
        return maxCode;
    }

}
